package me.zhengjie.sqlservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.mapper.WcsDeliveryMissionMapper;
import me.zhengjie.pojo.entity.WcsDeliveryMission;
import me.zhengjie.sqlservice.WcsDeliveryMissionService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class WcsDeliveryMissionServiceImpl extends ServiceImpl<WcsDeliveryMissionMapper, WcsDeliveryMission> implements WcsDeliveryMissionService {
    private final WcsDeliveryMissionMapper wcsDeliveryMissionMapper;

    @Override
    public boolean insertDelivery(WcsDeliveryMission wcsDeliveryMission) {
        return wcsDeliveryMissionMapper.insert(wcsDeliveryMission) >0;
    }

    @Override
    public WcsDeliveryMission selectDeliveryCode(String deliveryCode) {
        LambdaQueryWrapper<WcsDeliveryMission> wcsDeliveryMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsDeliveryMissionLambdaQueryWrapper.eq(WcsDeliveryMission::getDeliveryCode,deliveryCode);
        return wcsDeliveryMissionMapper.selectOne(wcsDeliveryMissionLambdaQueryWrapper);
    }

    @Override
    public boolean updateDeliveryStatusAndAgvId(Integer id, Integer missionStatus, String agvId) {
        LambdaUpdateWrapper<WcsDeliveryMission> wcsDeliveryMissionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsDeliveryMissionLambdaUpdateWrapper.set(WcsDeliveryMission::getMissionStatus,missionStatus)
                .set(WcsDeliveryMission::getAgvId,agvId)
                .set(WcsDeliveryMission::getUpdateTime, LocalDateTime.now())
                .eq(WcsDeliveryMission::getId,id);
        return wcsDeliveryMissionMapper.update(null,wcsDeliveryMissionLambdaUpdateWrapper) >0;
    }

    @Override
    public boolean updateMissionStatus(String deliveryCode, Integer status) {
        LambdaUpdateWrapper<WcsDeliveryMission> wcsDeliveryMissionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsDeliveryMissionLambdaUpdateWrapper.set(WcsDeliveryMission::getMissionStatus,status)
                .set(WcsDeliveryMission::getUpdateTime,LocalDateTime.now())
                .eq(WcsDeliveryMission::getDeliveryCode,deliveryCode);
        return wcsDeliveryMissionMapper.update(null,wcsDeliveryMissionLambdaUpdateWrapper) >0;
    }

    @Override
    public boolean deleteDeliveryId(Integer id) {
        return wcsDeliveryMissionMapper.deleteById(id) >0;
    }

    @Override
    public List<WcsDeliveryMission> selectMissionStatus(Integer status) {
        LambdaQueryWrapper<WcsDeliveryMission> wcsDeliveryMissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsDeliveryMissionLambdaQueryWrapper.eq(WcsDeliveryMission::getMissionStatus,status);
        return wcsDeliveryMissionMapper.selectList(wcsDeliveryMissionLambdaQueryWrapper);
    }
}
